class goods {
  constructor () {

  }

  async get (c) {
    let g = await c.service.model.goods.get(c.param.id);
    if (g === null) {
      c.res.body = {
        status : 'FAILED',
        errmsg : '没有发现商品。'
      };
      return ;
    }
    g.goods_image = this.parseImages(g.goods_image,c.service.config.domain);
    c.res.body = {
      status : 'OK',
      data : g
    };
  }

  parseImages (imagetext, host) {
    var images = imagetext.split(',').filter(p => p.length);
    for (let i=0; i<images.length; i++) {
      images[i] = `${host}/common/image/${images[i]}`;
    }
    return images;
  }

  async list (c) {
    let page = c.query.page ? c.query.page : 1;
    let kwd = c.query.kwd ? c.query.kwd : '';
    let glist = await c.service.model.goods.list(page, kwd);

    for (let i=0; i<glist.length; i++) {
      glist[i].goods_image = this.parseImages(glist[i].goods_image, c.service.config.domain);
    }

    c.res.body = {
      status : 'OK',
      data : glist
    };
  }

}

module.exports = goods;
